/*
 * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 *
 */
package gchisto.gui.panels.gcdata;

import gchisto.gcactivity.GCActivity;
import gchisto.gcactivity.GCActivitySet;
import gchisto.gctrace.GCTrace;
import gchisto.gctrace.GCTraceCheckpoint;
import gchisto.gctrace.RCWithGCTraceCheckpoint;
import gchisto.gctrace.GCTraceListener;
import gchisto.gctrace.RCWithGCTraceCheckpointCallback;
import gchisto.utils.Locker;
import gchisto.utils.Refresher;
import gchisto.utils.WorkerThread;
import gchisto.utils.errorchecking.ArgumentChecking;

/**
 *
 * @author  tony
 */
public class DataPanel extends javax.swing.JPanel
        implements GCTraceListener, RCWithGCTraceCheckpointCallback {

    final private GCTrace gcTrace;
    final private Refresher refresher;
    final private Locker locker = new Locker();

    String getString(GCActivity gcActivity) {
        return String.format("%20s %12.4f %12.6f\n",
                gcActivity.getName(),
                gcActivity.getStartSec(),
                gcActivity.getDurationSec());
    }

    private void updateTextArea(int from, int to) {
        GCActivitySet allGCActivities = gcTrace.getAllGCActivities();
        StringBuilder strBuilder = new StringBuilder();
        for (int i = from; i < to; ++i) {
            GCActivity gcActivity = allGCActivities.get(i);
            String str = getString(gcActivity);
            strBuilder.append(str);
        }
        textArea.append(strBuilder.toString());
    }

    public void refresh(GCTraceCheckpoint checkpoint) {
        int from = checkpoint.prevAllGCActivitiesSize();
        int to = checkpoint.allGCActivitiesSize();
        updateTextArea(from, to);
    }

    public void possiblyRefresh() {
        refresher.possiblyRefresh();
    }

    public void gcActivityAdded(
            GCTrace gcTrace,
            GCActivitySet gcActivitySet,
            GCActivity gcActivity) {
        assert gcTrace == this.gcTrace;
        possiblyRefresh();
    }

    public void gcActivityNameAdded(
            GCTrace gcTrace,
            int id,
            String gcActivityName) {
    }

    DataPanel(GCTrace gcTrace) {
        ArgumentChecking.notNull(gcTrace, "gcTrace");

        this.gcTrace = gcTrace;
        GCTraceCheckpoint checkpoint = new GCTraceCheckpoint(gcTrace);
        this.refresher = new Refresher(
                WorkerThread.instance(),
                new RCWithGCTraceCheckpoint(checkpoint, locker, this));
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        textAreaPane = new javax.swing.JScrollPane();
        textArea = new javax.swing.JTextArea();

        textArea.setColumns(20);
        textArea.setRows(5);
        textAreaPane.setViewportView(textArea);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(textAreaPane, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(textAreaPane, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea textArea;
    private javax.swing.JScrollPane textAreaPane;
    // End of variables declaration//GEN-END:variables
}
